<?php
/**
 * 新人大礼包管理
 * edition ：v1
 * create time：2021-06-08
 * author: acker
 */
namespace App\Admin\Controllers\Coupon;


use App\Http\Controllers\Controller;
use App\Models\yb\CouponPackageModel;
use App\Models\yb\CouponModel;
use Encore\Admin\Form;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use Encore\Admin\Show;


class PackageController extends Controller
{
    use ModelForm;

    protected $header = '新人大礼包管理';

    /**
     * 
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('列表');
            $content->body($this->grid());

        });
    }

    /**
     * 新增
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('添加');
            $content->body($this->form());

        });
    }

    /**
     * 编辑
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('编辑');
            $content->body($this->form()->edit($id));

        });
    }

    public function grid(){

        $grid = new Grid(new CouponPackageModel());

        $grid->model()->orderBy('created_at', 'DESC');

        $grid->column('id','编号')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->column('title','大礼包名称')->display(function($filed){
            return empty($filed) ? config('view.EmptyData') : $filed;
        });

        $grid->paginate(10);

        return $grid;
    }

    /**
     * @return mixed
     */
    public function form(){

        $form = new Form(new CouponPackageModel());

        $form->text('title', '礼包名称')->rules('required|max:150');

        $form->multipleSelect('coupon_id', '优惠券')->options(CouponModel::selectPackageOptions())->rules('required');

        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
            $tools->disableList();
            $tools->disableDelete();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            $footer->disableEditingCheck();
            $footer->disableCreatingCheck();
        });
        
        //保存后回调
        $form->saved(function (Form $form) {
            return redirect('/admin/coupon/package/manage/1/edit');
        });

        return $form;
    }

  
}